package com.example.a022.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
 import android.view.ViewGroup;

 /**
 * Created by 022 on 2018/9/3.
 */

public class CustomViewPager extends ViewGroup {
    private static final String TAG = CustomViewPager.class.getSimpleName();
    private float startX = 0;
    private GestureDetector detector;
    int a = 30;
    public CustomViewPager(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    private void init(Context context) {
        detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            /**
             * distanceX 在屏幕上要移动的距离 而不是坐标
             */
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                scrollBy(0, (int)distanceY);
                return  true;
            }
        });
    }
    public CustomViewPager(Context context) {
        super(context);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for(int i=0;i<getChildCount();i++){
            View childView = getChildAt(i);
//				childView.layout(i*getWidth(), 0, (i+1)*getWidth(), getHeight());、//这是水平方向滑动
            childView.layout(0, i*getHeight(), getWidth(), (i+1)*getHeight());//这是竖直方向滑动
        }
    }
    /**
     * 这个默认super.onTouchEvent(event)为false
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        return true;
    }

}
